package org.lql.chaos.rocketmq.service.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

/**
 * @author: liangqinglong
 * @date: 2025-08-16 20:19
 * @description: 事务消息生产者
 **/
@Slf4j
@Service
public class TransactionProducer {

	@Autowired
	private RocketMQTemplate rocketMQTemplate;

	/**
	 * 发送事务消息
	 *
	 * @param topic 主题
	 * @param tag   标签
	 * @param msg   消息内容
	 * @param arg   事务参数
	 */
	public void sendTransactionMessage(String topic, String tag, String msg, Object arg) {
		String destination = topic + ":" + tag;
		log.info("发送事务消息：{}", msg);

		Message<String> message = MessageBuilder.withPayload(msg).build();
		// 发送事务消息，第二个参数是事务执行参数
		rocketMQTemplate.sendMessageInTransaction(destination, message, arg);
	}
}